/*
 * @Author: TT-hzy
 * @Date: 2024-06-28 10:45:22
 * @LastEditors: TT-hzy 
 * @LastEditTime: 2024-07-05 15:51:27
 * @Description:
 */

import { Service } from "egg";


class User extends Service {
  async list({
    offset = 0,
    limit = 10,
  }: {
    offset: number;
    limit: number,
  }) {
    return this.ctx.model.User.findAndCountAll({
      offset,
      limit,
      order: [
        ["createdAt", "desc"],
        ["id", "desc"],
      ],
    });
  }



  async register(info: any) {

    return await this.ctx.model.User.create(info);
  }

  async login(info: any) {
    const { service } = this;
    const user = await service.common.getUserInfoByNameAndPassword(info.name, info.password);
    return user;
  }



  async update({ id, updates }: { id: number; updates: any }) {
    const {
      ctx
    } = this;
    const result = await ctx.model.User.update(updates, {
      where: { id },
    });
    var res = 0;
    if (result.length >= 1) {
      res = result[0];
    }
    return res;
  }

  delect({ id }: { id: number }) {
    const {
      ctx
    } = this;
    return ctx.model.User.destroy({ where: { id } });
  }

}

export default User;
